const md5 = require("md5");

const judgeForm = (formDataObj) => {
    var resObj = {
        flag: true,
    };
    // 如果 formDataObj.username 存在的话就进行验证该属性的合格性
    if (
        formDataObj.username &&
        (formDataObj.username.length < 3 ||
            formDataObj.username.length > 30 ||
            /\W/.test(formDataObj.username))
    ) {
        resObj = {
            flag: false,
            message: "用户名长度需要大于等于3位小于等于30位,并且不能包含特殊字符",
        };
        return resObj;
    }

    // 如果 formDataObj.password 存在
    if (
        formDataObj.password &&
        (formDataObj.password.length < 6 || formDataObj.password.length > 16)
    ) {
        resObj = {
            flag: false,
            message: "密码长度需要大于等于6位小于等于16位",
        };
        return resObj;
    }

    // 密码确认
    if (formDataObj.ensureP && formDataObj.password != formDataObj.ensureP) {
        resObj = {
            flag: false,
            message: "两次密码不一样",
        };
        return resObj;
    }

    if (
        formDataObj.mobile &&
        !/^(?:(?:\+|00)86)?1[3-9]\d{9}$/.test(formDataObj.mobile)
    ) {
        resObj = {
            flag: false,
            message: "电话号码格式错误",
        };
        return resObj;
    }

    return resObj;
};

let tools = {
    md5,
    judgeForm,
};

module.exports = tools;